package com.gugee.tiktok.web.service;

import com.gugee.tiktok.web.dto.LanguageResponse;
import com.gugee.tiktok.web.dto.RegionResponse;
import com.gugee.tiktok.web.model.mysql.General;
import com.gugee.tiktok.web.repository.DicLanguageRepository;
import com.gugee.tiktok.web.repository.DicRegionRepository;
import com.gugee.tiktok.web.repository.GeneralRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class CommonService {

    @Autowired
    DicRegionRepository dicRegionRepository;

    @Autowired
    RegionResponse.BloggerMapper regionMapper;
    @Autowired
    DicLanguageRepository dicLanguageRepository;

    @Autowired
    LanguageResponse.BloggerMapper languageMapper;

    @Autowired
    GeneralRepository generalRepository;

    public List<RegionResponse> allRegions;
    public List<RegionResponse> headRegions;

    public List<LanguageResponse> allLanguages;
    public List<LanguageResponse> headLanguages;

    @PostConstruct
    public void postConstruct(){
        allRegions = regionMapper.mapper(dicRegionRepository.findByOrderByName());
        headRegions = regionMapper.mapper(dicRegionRepository.findByTypeOrderByName(1));
        allLanguages = languageMapper.mapper(dicLanguageRepository.findByOrderById());
        headLanguages = languageMapper.mapper(dicLanguageRepository.findByTypeOrderById(1));
    }

    public List<RegionResponse> getRegion(Integer type){
        if(null == type){
            return allRegions;
        }else if(1 == type){
            return headRegions;
        }
        return allRegions;
    }

    public List<LanguageResponse> getLanguage(Integer type){
        if(null == type){
            return allLanguages;
        }else if(1 == type){
            return headLanguages;
        }
        return allLanguages;
    }

    public List<General> getGeneralByType(String type){
        return generalRepository.findByType(type);
    }
}
